<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #fff9e6;  /* 浅黄色背景 */
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .login-container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
        }

        h2 {
            text-align: center;
            color: #b8860b;  /* 深黄色标题 */
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #8b4513;  /* 深文字 */
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ffd700;  /* 金黄色边框 */
            border-radius: 4px;
            box-sizing: border-box;
            outline: none;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            border-color: #daa520;  /* 深金黄色聚焦边框 */
            box-shadow: 0 0 5px rgba(218,165,32,0.3);
        }

        .captcha-group {
            display: none;  /* 默认隐藏验证码 */
            margin-bottom: 20px;
        }

        .captcha-input {
            width: 60% !important;
            margin-right: 10px;
        }

        .captcha-image {
            border: 1px solid #ffd700;
            border-radius: 4px;
            transition: all 0.3s ease;
        }

        .captcha-image:hover {
            border-color: #ffd700 !important;
        }

        .login-btn {
            width: 100%;
            padding: 12px;
            background-color: #ffd700;  /* 金黄色按钮 */
            color: #8b4513;  /* 深棕色文字 */
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.3s ease;
        }

        .login-btn:hover {
            background-color: #daa520;  /* 深金黄色悬停效果 */
            color: white;
        }

        .register-link {
            text-align: center;
            margin-top: 20px;
        }

        .register-link a {
            color: #daa520;
            text-decoration: none;
            transition: color 0.3s ease;
        }

        .register-link a:hover {
            color: #b8860b;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>用户登录</h2>
        <div class="login-form">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username">
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password">
            </div>
            <div class="captcha-group" id="captchaGroup" style="display: none;">
                <label for="captcha">验证码</label>
                <div style="display: flex; align-items: center;">
                    <input type="text" id="captcha" name="captcha" class="captcha-input" autocomplete="off">
                    <img src="/kaptcha" class="captcha-image" id="captchaImage" 
                         onclick="refreshCaptcha()" alt="点击刷新验证码" 
                         style="height: 40px; cursor: pointer; margin-left: 10px; border: 1px solid #ddd;">
                </div>
            </div>
            <button class="login-btn" onclick="login()">登录</button>
        </div>
        <div class="register-link">
            还没有账号？<a href="register.html">立即注册</a>
        </div>
    </div>

    <script>
        function login() {
            event.preventDefault();
            
            const username = $('#username').val().trim();
            const password = $('#password').val();

            if (!username || !password) {
                alert('请输入用户名和密码');
                return;
            }

            // 构建基本的登录数据
            const loginData = {
                username: username,
                password: password
            };

            // 只有当验证码输入框显示时，才添加验证码
            if ($('#captchaGroup').is(':visible')) {
                const captcha = $('#captcha').val();
                if (!captcha) {
                    alert('请输入验证码');
                    return;
                }
                loginData.captcha = captcha;
            }

            $.ajax({
                url: '/user/login',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(loginData),
                success: function(response) {
                    console.log('登录响应:', response);
                    if (response.state === 1) {
                        location.href = 'home.html';
                    } else {
                        // 根据错误消息判断是否需要显示验证码
                        if (response.msg.includes('请输入验证码')) {
                            $('#captchaGroup').show();
                            refreshCaptcha();
                        }
                        alert(response.msg);
                    }
                },
                error: function(xhr, status, error) {
                    console.error('登录失败:', error);
                    console.error('状态:', status);
                    console.error('响应:', xhr.responseText);
                    alert('登录失败，请稍后重试');
                }
            });
        }

        function refreshCaptcha() {
            $('#captchaImage').attr('src', '/kaptcha?' + new Date().getTime());
        }

        // 页面加载完成后初始化
        $(document).ready(function() {
            $('#captchaGroup').hide();
        });

        // 监听回车键
        $(document).keypress(function(e) {
            if (e.which == 13) {
                login();
            }
        });
    </script>
</body>
</html>
